package ca.uwindsor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class AddMarks extends Activity {

	String course_code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_marks);

		// populate spinner
		fillSpinner(Common.globalTerm);

		Button add_but = (Button) findViewById(R.id.add_button);

		Spinner sCourseCode = (Spinner) findViewById(R.id.mrkSpinnerCode);
		sCourseCode.setOnItemSelectedListener(new MyOnItemSelectedListener());

		// Alert Dialog builder
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);

		add_but.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				
				final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
				db.setDATABASE_TABLE("marks"); //set database table

				db.open();

				EditText marks = (EditText) findViewById(R.id.marks_ob);
				EditText totalMarks = (EditText) findViewById(R.id.total_mrk);
				EditText weight = (EditText) findViewById(R.id.weight);
				//check if total weight of the course is greater than 100
				Cursor weightChk = db.getEntry("course_code",course_code);
				Double totalWeight = Double.parseDouble(weight.getText().toString());
				while(weightChk.moveToNext()){
					totalWeight += weightChk.getDouble(4);
				}
				
				
				if(marks.getEditableText().toString().trim().equals("") ||
						totalMarks.getEditableText().toString().trim().equals("") ||
						weight.getEditableText().toString().trim().equals(""))
						
				{
					alert.setMessage("All fields are required");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
				}
				else if(Double.parseDouble(marks.getEditableText().toString()) >
				Double.parseDouble(totalMarks.getEditableText().toString()))
						
				{
					alert.setMessage("Total marks should be greater than marks ob.");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
				}
				else if(totalWeight>100){
					//show alert to user
					alert.setMessage("Total weightage for this course is greater than 100.");

					alert.setNegativeButton("OK",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									//do nothing
								}

							});
					alert.show();
				}
				else{
					//add to database
				Double dMarks = Double.parseDouble(marks.getText().toString());
				Double dTotalMarks = Double.parseDouble(totalMarks.getText().toString());
				Double dWeight = Double.parseDouble(weight.getText().toString());
				Double cal = (dMarks/dTotalMarks) * dWeight;
				//add to database
				ContentValues newValues = new ContentValues();

				newValues.put("course_code", course_code);

				newValues.put("marks", marks.getText().toString());

				newValues.put("total_marks", totalMarks.getText().toString());

				newValues.put("weightage", weight.getText().toString());
				
				newValues.put("marks_ob", cal.toString());

				db.insertEntry(newValues);

				alert.setMessage("Marks added for course " + course_code);

				alert.setNegativeButton("OK",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								startActivity(new Intent("ca.uwindsor.SUMMARY"));
							}

						});
				alert.setCancelable(false);
				alert.show();
				}
				db.close();
			}

		});
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	public void fillSpinner(String term) {
		MyDBAdapter db = new MyDBAdapter(this);
		db.setDATABASE_TABLE("courses");
		db.open();
		// create an array adapter
		ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
				this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// gets all the entries
		Cursor c = db.getEntry("sem_name", term);

		final AlertDialog.Builder alert = new AlertDialog.Builder(this);

		// if no courses exist forward to add courses
		if (c.getCount() == 0) {
			alert.setMessage("No courses added yet. Please add courses first.");

			alert.setNegativeButton("OK",
					new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							startActivity(new Intent("ca.uwindsor.ADDCOURSES"));
						}

					});

			db.close();
			alert.setCancelable(false);
			alert.show();
		}

		if (c.moveToFirst()) {
			do {
				adapter.add(c.getString(2));
			} while (c.moveToNext());
		}
		Spinner s = (Spinner) findViewById(R.id.mrkSpinnerCode);
		s.setAdapter(adapter);
		db.close();
	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			// System.out.print(parent.getAdapter().toString());
			switch (parent.getId()) {
			case R.id.mrkSpinnerCode:
				course_code = parent.getItemAtPosition(pos).toString();
				break;

			}

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}
}
